package com.mytools.tool.views;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.mytools.tool.utils.StringUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyPictureDialog extends Dialog {

	public MyPictureDialog(Context context) {
		super(context);
	}

	public MyPictureDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String title;
		private String pictureUrl;
		private View contentView;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}
		
		public Builder setPicture(String pictureUrl){
			this.pictureUrl = pictureUrl;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public MyPictureDialog create() {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final MyPictureDialog dialog = new MyPictureDialog(context, R.style.Dialog);
			View layout = inflater.inflate(R.layout.dialog_picture, null);
			
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			
			if(StringUtil.isNull(title)){
				((TextView) layout.findViewById(R.id.tv_title)).setVisibility(StringUtil.isNull(title)?View.GONE:View.VISIBLE);
			}
			((TextView) layout.findViewById(R.id.tv_title)).setText(title);
			
			ImageView imageView = (ImageView)layout.findViewById(R.id.img_picture);
			ImageLoader.getInstance().displayImage(pictureUrl, imageView);
			
			if (contentView != null) {
				((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
				((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
			}
			dialog.setContentView(layout);
			return dialog;
		}
	}
}
